/*
 * Created on 2006-2-22
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cn.com.sbell.vss.csg.vap;

import org.dom4j.Element;

import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.csg.vap.bean.SerialPortInfo;
import cn.com.sbell.vss.csg.vsclient.VsClient;
import cn.com.sbell.vss.csg.vsclient.dahua.DaHuaVsClient;
import cn.com.sbell.vss.csg.vsclient.hikvision.HikVisionDVRClient;
import cn.com.sbell.vss.csg.vsclient.util.PropertiesHolder;

/**
 * @author liang.a.chen
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class GetVsSerialPort extends ProtectedVsCommander {

	protected Element executeCommand(VsClient vsClient, PropertiesHolder holder, Element root) {
		Integer port = holder.getIntProperty("port");
		if(port == null){
			throw new VssException(VssConstants.CODE_REQUIRED,new String[]{"csg.port"});
		}
		SerialPortInfo result = null;
		if(port.intValue() == 2 && vsClient instanceof HikVisionDVRClient ){
//			Integer decoderAddress = holder.getIntProperty("decoderAddress");
//			if(decoderAddress == null){
//				throw new VssException(VssConstants.CODE_REQUIRED,new String[]{"csg.decoderAddress"});
//			}			
//			result = vsClient.getDecoderCfg(decoderAddress.intValue());
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//vondart changed
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			Integer videoId = holder.getIntProperty("videoId");
			if(videoId == null){
				throw new VssException(VssConstants.CODE_REQUIRED,new String[]{"csg.videoId"});
			}
			result = vsClient.getDecoderCfg(videoId.intValue());
			
		}else if(port.intValue() == 2 && vsClient instanceof DaHuaVsClient){
			Integer videoId = holder.getIntProperty("videoId");
			if(videoId == null){
				throw new VssException(VssConstants.CODE_REQUIRED,new String[]{"csg.videoId"});
			}
			result = vsClient.getDecoderCfg(videoId.intValue());
		}else{
			result = vsClient.getSerialPortInfo(port.intValue());
		}
		
		root.addElement("port").addText(toString(result.getPort()));
		root.addElement("baudRate").addText(toString(result.getBaudRate()));
		root.addElement("dataBit").addText(toString(result.getDataBit()));
		root.addElement("parity").addText(toString(result.getParity()));
		root.addElement("stopBit").addText(toString(result.getStopBit()));
		root.addElement("videoId").addText(toString(result.getVideoId()));
		root.addElement("mode").addText(toString(result.getMode()));
//		
		root.addElement("timeout").addText(toString(result.getTimeout()));
		root.addElement("flowcontrol").addText(toString(result.getFlowcontrol()));
		root.addElement("decoderType").addText(toString(result.getDecoderType()));
		root.addElement("decoderAddress").addText(toString(result.getDecoderAddress()));
		root.addElement("workMode").addText(toString(result.getWorkMode()));
		
		return root;
	}

}
